package xmlparser;

import java.util.StringTokenizer;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class SubXmlHandler extends DefaultHandler {

    public int id;
    public String name;
    public int free;
    public int occupied;

    private int divCounter;

    @Override
    public void startDocument() throws SAXException {

	divCounter = 0;
	name = "";
    }

    @Override
    public void startElement(String uri, String localName, String qName,
	    Attributes attributes) throws SAXException {

    }

    @Override
    public void endElement(String uri, String localName, String qName)
	    throws SAXException {

    }

    @Override
    public void characters(char[] ch, int start, int length)
	    throws SAXException {

	String tmp = new String(ch, start, length);
	// name += "(" + divCounter + " - " + tmp + ")" + " ";

	try {
	    switch (divCounter) {

	    case 0: // id and dock name

		// name += "(" + divCounter + " - " + tmp + ")" + " ";

		StringTokenizer st = new StringTokenizer(tmp);
		id = Integer.valueOf(st.nextToken("-").trim());
		name = st.nextToken();
		break;
	    case 3: // occupied
		    // name += "(" + divCounter + " - " + tmp + ")" + " ";
		occupied = Integer.valueOf(tmp.trim());
		break;
	    case 4: // free
		    // name += "(" + divCounter + " - " + tmp + ")" + " ";
		free = Integer.valueOf(tmp.trim());
		break;
	    }
	} catch (Exception e) {
	    
	    name = e.getMessage();
	}

	divCounter++;
    }
}
